package com.lineage.server.command.executor;

import com.lineage.server.EchoServerTimer;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Echo implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1Echo.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1Echo();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      if (arg.equalsIgnoreCase("stop")) {
        if (pc == null) {
          _log.warn("系统命令执行: 全部端口关闭监听!");
        } else {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "全部端口关闭监听!"));
        } 
        EchoServerTimer.get().stopEcho();
        return;
      } 
      String info = "重新启动服务器端口监听!";
      _log.info("重新启动服务器端口监听!");
      EchoServerTimer.get().reStart();
      if (pc != null)
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "重新启动服务器端口监听!")); 
    } catch (Exception e) {
      if (pc == null) {
        _log.error("错误的命令格式: " + getClass().getSimpleName());
      } else {
        _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
      } 
    } finally {
      _log.info("监听端口设置作业完成!!");
    } 
  }
}
